package com.example.administrator.myapplication;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private TextView text_result;
    private ImageView imageView;
    private final String TAG = "MainActivity";
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text_result = findViewById(R.id.text_result);
        imageView=findViewById(R.id.iv_img);
        requestQueue = Volley.newRequestQueue(MainActivity.this);
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                useVolleyJsonRequest();
            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             /*   useVolleyStringRequest("13353054740");*/
                useVolleyStringRequestPOST("13353054740");
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                useVolleyImageRequest();
            }
        });
    }

    private void useVolleyImageRequest() {
        String url="http://jsxy.gdcp.cn/UploadFile/2/2019/5/8/20195823312370.jpg";
        ImageRequest imageRequest=new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
            }
        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG,""+volleyError.toString());
            }
        });
        requestQueue.add(imageRequest);
    }


    private void useVolleyStringRequestPOST(final String phone) {
        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";

        StringRequest stringRequest=new StringRequest(Request.Method.POST, phoneBaseUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG, s);
                text_result.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map=new HashMap<>();
                map.put("mobileCode",phone);
                map.put("userID","");
                return map;
            }
        };
        requestQueue.add(stringRequest);
    }

    private void useVolleyJsonRequest() {
        String url = "https://www.tianqiapi.com/api/";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
            Log.e(TAG,jsonObject.toString());
                try {
                    String wea=jsonObject.getJSONArray("data").getString(1);

                    StringBuffer stringBuffer=new StringBuffer();
                    stringBuffer.append(wea);

                    text_result.setText(stringBuffer.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
        Log.e(TAG,volleyError.toString());
            }
        });
        requestQueue.add(jsonObjectRequest);
    }


    private void useVolleyStringRequest(String phone) {
        String phoneBaseUrl="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String phoneUrl=phoneBaseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");

        /*String url = "http://www.baidu.com";*/
        StringRequest stringRequest = new StringRequest(phoneUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG, s);
                text_result.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());

            }
        });
        requestQueue.add(stringRequest);
    }
}
